

from settings import FIELDCLASSMAPPING

from util.g import g
from PageObjectRequests.WriteApplicationCover import WriteApplicationCover
from PageObjectRequests.WriteHrForm import WriteHRForm
from PageObjectRequests.WriteIpForm import WriteIPForm
from PageObjectRequests.WriteRdForm import WriteRDForm
from PageObjectRequests.WriteRdcostForm import WriteRdcostForm
from PageObjectRequests.WritePsForm import WritePSForm
from PageObjectRequests.write_innovation_ability_form import WriteInnovationAbilityForm
from PageObjectRequests.WriteStandForm import WriteStandForm
from PageObjectRequests.UploadAccessory import UploadAccessory
from PageObjectRequests.WriteMaincaseForm import WriteMaincaseForm
from PageObjectRequests.WriteRegistrationForm import WriteRegistrationForm
# from PageObject.WriteApplicationCover import WriteApplicationCover
# from PageObject.WriteHrForm import WriteHRForm
# from PageObject.WriteIpForm import WriteIPForm
# from PageObject.WriteRdForm import WriteRDForm
# from PageObject.WriteRdcostForm import WriteRdcostForm
# from PageObject.WritePsForm import WritePSForm
# from PageObject.write_innovation_ability_form import WriteInnovationAbilityForm
# from PageObject.WriteStandForm import WriteStandForm
# from PageObject.UploadAccessory import UploadAccessory
# from PageObject.WriteMaincaseForm import WriteMaincaseForm
# from PageObject.WriteRegistrationForm import WriteRegistrationForm


def write(driver):
    # # # # # # # # # # # # # # # #   点击 添加按钮之前，先显式等待 添加按钮出现。保存操作花费时间较长。
    # # # # # # # # # # # # # # # #   在有添加按钮的表单页面时，调用等待方法直接等待添加按钮，。
    # # # # # # # #  在有添加按钮的表单中 点击添加按钮之后 等待 保存按钮出现 再进行表单填写。

    # # # # # # # # # 填写申请书封皮
    WriteApplicationCover(driver).main()
    driver.switch_to.parent_frame()

    # # #  # # # # # # # 填写注册登记表
    WriteRegistrationForm(driver).main()
    driver.switch_to.parent_frame()

    # Request URL: https://slb.chinatorch.org.cn/xonlinereport/entUserReg/UserInfo/getMainDomain.do

    #     #           #        知识产权汇总表。
    # # # 方法一： 填写完数据后对输入框的 value 属性进行检测， 如果 value为空， 使用 js 给输入框的 value 属性赋值。

    #  # #  解决方案， 填写完数据后进行检测， 如果检测到 value 为空， 则 使用 js 对元素的 value 赋值。
    # # 使用 执行 js 代码， 结合 每次 0.5 秒的时间等待， 该 bug 已经比较完美的解决了。
    WriteIPForm(driver=driver).main()
    driver.switch_to.parent_frame()

    # 人力资源表填写。
    WriteHRForm(driver=driver).main()
    driver.switch_to.parent_frame()

    # # # # ##############################   填写   企业研究开发活动情况表
    WriteRDForm(driver=driver).main()
    driver.switch_to.parent_frame()

    #  # # # # # # # # # # #   填写   企业年度研究开发费用结构明细表
    WriteRdcostForm(driver=driver).main()
    driver.switch_to.parent_frame()

    # ##############################   填写   上年度高新技术产品（服务）情况表
    WritePSForm(driver=driver).main()
    driver.switch_to.parent_frame()

    # ##############################   填写   主要情况表
    WriteMaincaseForm(driver=driver).main()
    driver.switch_to.parent_frame()

    # # # # # # # # # # # # #  企业创新能力
    WriteInnovationAbilityForm(driver=driver).main()
    driver.switch_to.parent_frame()

    # # ##############################   填写   企业参与国家标准或行业标准制定情况汇总表
    WriteStandForm(driver=driver).main()
    driver.switch_to.parent_frame()

    # ##############################   填写   上传附件
    UploadAccessory(driver=driver).main()

def write_portion(driver, record=None):
    if not record:
        write(driver)
    else:
        for index, i in enumerate(FIELDCLASSMAPPING):
            if not i:
                continue
            if getattr(record, i) != 1:
                model = FIELDCLASSMAPPING.get(i).get("restart_model")
                # page = globals()[FIELDCLASSMAPPING.get(i).get("restart_model")](driver=driver)
                if model not in g.models:
                    g.models.append(model)
                    page_class = globals().get(model)
                    page = page_class(driver=driver)
                    page.main()
                    if index != len(FIELDCLASSMAPPING) - 1:
                        driver.switch_to.parent_frame()

if __name__ == '__main__':

    """"""
    # a = locals().get("WriteRegistrationForm")
    # b = a(driver="a")
    # b()
    # a = globals().get("WriteRegistrationForm")
    # print(a)
    for i in FIELDCLASSMAPPING:
        page_class = locals().get(FIELDCLASSMAPPING.get(i).get("restart_model"))
        page = page_class(driver="a")
        print(page)

